home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE12 / CONSTRUC / TBHEXVU.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-07-17  |  5.6 KB  |  199 lines

  1. unit TBHEXVU;
  2. {$I+}
  3. interface
  4. uses
  5.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms,
  6.   Dialogs, StdCtrls, Grids;
  7.  
  8. Const
  9.   BlockLine = 16;
  10.   BlockChar = 16;
  11.   BlockSize = BlockLine * BlockChar; { 16 x 16 = 256 }
  12.  
  13. Type
  14.   TBlock = Array[1..BlockSize] of Byte;
  15.  
  16.   TBHexViewer = class(TStringGrid)
  17.     private
  18.       { Private declarations }
  19.       FFileName: TFileName;
  20.       procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  21.     protected
  22.       { Protected declarations }
  23.       FFile: File;
  24.       FOffset: LongInt; { 0, 256, 512, ... 2G }
  25.       FBlock: TBlock; { data from FFile }
  26.       FSize: Cardinal; { actual size of data in FBlock }
  27.       FAbout: String;
  28.       procedure SetFileName(AFileName: TFileName); virtual;
  29.       procedure SetOffset(AnOffset: LongInt); virtual;
  30.       procedure SetSize(unused: Cardinal); { do nothing }
  31.       procedure SetAbout(unused: String); { do nothing }
  32.       function SelectCell(ACol, ARow: Longint): Boolean; override;
  33.       { this function does *not* work when declared private... }
  34.     public
  35.       { Public declarations }
  36.       constructor Create(AOwner: TComponent); override;
  37.       destructor Destroy; override;
  38.     published
  39.       { Published declarations }
  40.       property FileName: TFileName read FFileName write SetFileName;
  41.       property Offset: LongInt read FOffset write SetOffset default 0;
  42.       property Size: Cardinal read FSize write SetSize default 0;
  43.       property About: String read FAbout write SetAbout;
  44.     end {TBHexViewer};
  45.  
  46. implementation
  47.  
  48.   constructor TBHexViewer.Create(AOwner: TComponent);
  49.   var i: Integer;
  50.   begin
  51.     inherited Create(AOwner);
  52.     FAbout := 'TBHexViewer (c) 1996 by Dr.Bob [100434,2072]-> see http://www.pi.net/~drbob/';
  53.     ParentFont := False;
  54.     Font.Name := 'Courier New';
  55.     Font.Size := 10;
  56.     Height := 342{+17};
  57.     Width := 632;
  58.     FFileName := '';
  59.     FOffset := 0;
  60.     FSize := 0;
  61.     ScrollBars := ssNone;
  62.     ColCount := 18;
  63.     RowCount := 17;
  64.     DefaultRowHeight := 19{+1};
  65.     Cells[$0,0] := 'offset';
  66.     Cells[$1,0] := '$1';
  67.     Cells[$2,0] := '$2';
  68.     Cells[$3,0] := '$3';
  69.     Cells[$4,0] := '$4';
  70.     Cells[$5,0] := '$5';
  71.     Cells[$6,0] := '$6';
  72.     Cells[$7,0] := '$7';
  73.     Cells[$8,0] := '$8';
  74.     Cells[$9,0] := '$9';
  75.     Cells[$A,0] := '$A';
  76.     Cells[$B,0] := '$B';
  77.     Cells[$C,0] := '$C';
  78.     Cells[$D,0] := '$D';
  79.     Cells[$E,0] := '$E';
  80.     Cells[$F,0] := '$F';
  81.     Cells[16,0] := '$0';
  82.     ColWidths[0] := 76;
  83.     for i:=1 to 16 do ColWidths[i] := 25;
  84.     ColWidths[17] := 136
  85.   end {Create};
  86.  
  87.   destructor TBHexViewer.Destroy;
  88.   begin
  89.     if FFileName <> '' then
  90.     begin
  91.       {$I-}
  92.       Close(FFile);
  93.       {$I+}
  94.       if IOResult <> 0 then { skip };
  95.     end;
  96.     inherited Destroy
  97.   end {Destroy};
  98.  
  99.   procedure TBHexViewer.SetFileName(AFileName: TFileName);
  100.   begin
  101.     if FFileName <> '' then
  102.     begin
  103.       FFileName := '';
  104.       FOffset := 0;
  105.       FSize := 0;
  106.       {$I-}
  107.       System.Close(FFile);
  108.       {$I+}
  109.       if IOResult <> 0 then { skip };
  110.     end;
  111.     System.Assign(FFile,AFileName);
  112.     try
  113.       FileMode := $42; { read/write, deny-none }
  114.       System.Reset(FFile,1);
  115.       FFileName := AFileName { success! }
  116.     except
  117.       FFileName := ''
  118.     end;
  119.     Offset := 0
  120.   end {SetFileName};
  121.  
  122.   procedure TBHexViewer.SetOffset(AnOffset: LongInt);
  123.   var i,j,k: Integer;
  124.       Line: String;
  125.   begin
  126.     AnOffset := AnOffset AND NOT BlockLine; { skip lower bits }
  127.     if (AnOffset <> FOffset) or (AnOffset = 0) or (FOffset = 0) then
  128.     begin
  129.       FOffset := AnOffset;
  130.       FillChar(FBlock,SizeOf(FBlock),#0);
  131.       try
  132.         if FFileName <> '' then
  133.         try
  134.           Seek(FFile,FOffset);
  135.           BlockRead(FFile,FBlock,SizeOf(FBlock),FSize);
  136.         except
  137.           FOffset := 0;
  138.           FSize := 0
  139.         end
  140.         else
  141.         begin
  142.           FOffset := 0;
  143.           FSize := 0
  144.         end;
  145.       finally
  146.         k := 0;
  147.         for i:=1 to BlockLine do
  148.         begin
  149.           Cells[0,i] := '$'+IntToHex(FOffset + Pred(i) * BlockChar,8);
  150.           for j:=1 to BlockChar do
  151.           begin
  152.             Inc(k);
  153.             if k <= FSize then Cells[j,i] := IntToHex(FBlock[k],2)
  154.                           else Cells[j,i] := ''
  155.           end;
  156.           Dec(k,BlockChar);
  157.           Line := '';
  158.           for j:=1 to BlockChar do
  159.           begin
  160.             Inc(k);
  161.             if k <= FSize then if FBlock[k] < 32 then Line := Line + ' '
  162.                                                  else Line := Line + Chr(FBlock[k])
  163.           end;
  164.           Cells[17,i] := Line
  165.         end
  166.       end
  167.     end
  168.   end {SetOffset};
  169.  
  170.   procedure TBHexViewer.SetSize(unused: Cardinal);
  171.   begin
  172.     { does nothing, but makes the Size property visible in the Object Inspector }
  173.   end {SetSize};
  174.  
  175.   procedure TBHexViewer.SetAbout(unused: String);
  176.   begin
  177.     { does nothing, but makes the About property visible in the Object Inspector }
  178.   end {SetAbout};
  179.  
  180.   function TBHexViewer.SelectCell(ACol, ARow: Longint): Boolean;
  181.   begin
  182.     Result := inherited SelectCell(ACol,ARow) and (ACol <> 17)
  183.   end {SelectCell};
  184.  
  185.   procedure TBHexViewer.KeyDown(var Key: Word; Shift: TShiftState);
  186.   begin
  187.     if Key = 34 then { PgDown }
  188.     begin
  189.       if Size = BlockSize then Offset := Offset + BlockSize
  190.     end
  191.     else if Key = 33 then { PgUp }
  192.     begin
  193.       if (Offset >= BlockSize) then Offset := Offset - BlockSize
  194.                                else Offset := 0
  195.     end;
  196.     inherited KeyDown(Key,Shift);
  197.   end {KeyDown};
  198. end.
  199.